﻿#include "hand_keys.h"
#include "tank.h"
hand_keys::hand_keys(QWidget *parent) : QWidget(parent){

}

hand_keys::hand_keys()
{

}


void hand_keys::recive_key(tank &t1,tank &t2){
    if (t1.isLive == 1) {
        if (GetAsyncKeyState('A') & 0x8000) {
            t1.angle -= 2;
            t1.count_xy(t1);
            if(t1.tank_to_wall(t1)){
                t1.angle+=2;
                t1.count_xy(t1);
            }
        }
        if (GetAsyncKeyState('D') & 0x8000) {
            t1.angle += 2;
            t1.count_xy(t1);
            if(t1.tank_to_wall(t1)){
                t1.angle-=2;
                t1.count_xy(t1);
            }
        }
        if (GetAsyncKeyState('W') & 0x8000) {
            t1.x = (2 * cos((90 + t1.angle) / 180.0 * pai)) + t1.x;
            t1.y = (2 * sin((90 + t1.angle) / 180.0 * pai)) + t1.y;
            t1.count_xy(t1);
            if(t1.tank_to_wall(t1)){
                t1.x = -(2 * cos((90 + t1.angle) / 180.0 * pai)) + t1.x;
                t1.y = -(2 * sin((90 + t1.angle) / 180.0 * pai)) + t1.y;
                t1.count_xy(t1);
            }

        }
        if (GetAsyncKeyState('S') & 0x8000) {
            t1.x = (-2 * cos((90 + t1.angle) / 180.0 * pai)) + t1.x;
            t1.y = (-2 * sin((90 + t1.angle) / 180.0 * pai)) + t1.y;
            t1.count_xy(t1);
            if(t1.tank_to_wall(t1)){
                t1.x = (2 * cos((90 + t1.angle) / 180.0 * pai)) + t1.x;
                t1.y = (2 * sin((90 + t1.angle) / 180.0 * pai)) + t1.y;
                t1.count_xy(t1);
            }

        }
    }
    if(select_view!=2){
        if (t2.isLive == 1) {
            if (GetAsyncKeyState(VK_UP) & 0x8000) {
                t2.x = (2 * cos((90 + t2.angle) / 180.0 * pai)) + t2.x;
                t2.y = (2 * sin((90 + t2.angle) / 180.0 * pai)) + t2.y;
                t2.count_xy(t2);
                if(t2.tank_to_wall(t2)){
                    t2.x = -(2 * cos((90 + t2.angle) / 180.0 * pai)) + t2.x;
                    t2.y = -(2 * sin((90 + t2.angle) / 180.0 * pai)) + t2.y;
                    t2.count_xy(t2);
                }
            }
            if (GetAsyncKeyState(VK_DOWN) & 0x8000) {
                t2.x = (-2 * cos((90 + t2.angle) / 180.0 * pai)) + t2.x;
                t2.y = (-2 * sin((90 + t2.angle) / 180.0 * pai)) + t2.y;
                t2.count_xy(t2);
                if(t2.tank_to_wall(t2)){
                    t2.x = (2 * cos((90 + t2.angle) / 180.0 * pai)) + t2.x;
                    t2.y = (2 * sin((90 + t2.angle) / 180.0 * pai)) + t2.y;
                    t2.count_xy(t2);
                }
            }
            if (GetAsyncKeyState(VK_LEFT) & 0x8000) {
                t2.angle -= 2;
                t2.count_xy(t2);
                if(t2.tank_to_wall(t2)){
                    t2.angle+=2;
                    t2.count_xy(t2);
                }
            }
            if (GetAsyncKeyState(VK_RIGHT) & 0x8000) {
                t2.angle += 2;
                t2.count_xy(t2);
                if(t2.tank_to_wall(t2)){
                    t2.angle-=2;
                    t2.count_xy(t2);
                }
            }
        }
    }
    if (GetAsyncKeyState(VK_ESCAPE) & 0x8000) {
        select_view=0;
    }
}
